package com.irany;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.setting.dialect.Props;
import com.irany.component.MyReportCollector;
import com.irany.model.UserInfo;
import com.irany.util.SeleniumUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;

import java.util.Scanner;

/**
 * @author shujian.ou
 * @since 2021/5/17 0017 16:10
 */
public class JiraApp extends App {

    public JiraApp(String appName) {
        initProp();
        this.appName = appName;
    }

    private void initDriver() {
        ChromeOptions options = new ChromeOptions();
      /*  options.addArguments("--headless");
        options.addArguments("--silent");
       */
        driver = SeleniumUtils.getDriver(this.appName, options);
        driver.get("http://jira.zmops.cc");
    }

    @Override
    protected void initProp() {
        appProps = Props.getProp("app.properties", CharsetUtil.UTF_8);
        userInfo = new UserInfo();
        userInfo.setUsername(appProps.getStr("app.jira.username"));
        userInfo.setPassword(appProps.getStr("app.jira.password"));
        userInfo.setFullName(appProps.getStr("app.jira.name"));
    }

    @Override
    public boolean run() throws Exception {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            if (isFirstRun()) {
                //第一次运行
                System.out.println("=========第一次运行,请先设置用户信息=========");
                scannerSetUserInfo();
                getAppProps().setProperty("app.isFirst", false);
                storeProp();
                scannerMenu();
            } else {
                scannerMenu();
            }
            int chose = scanner.nextInt();
            switch (chose) {
                case 1:
                    System.out.println("生成周报中...");
                    addCollector(new MyReportCollector(this));
                    initDriver();
                    if (super.run()) {
                        System.out.println("生成完成,周报已保存至程序根目录!");
                    }

                    System.out.println();
                    break;
                case 2:
                    scannerSetUserInfo();
                    break;

                case 3:
                    scannerHelp();
                    break;
                default:
                    return true;
            }
        }
    }

    private void scannerMenu() {
        System.out.println("1.生成周报");
        System.out.println("2.修改用户信息");
        System.out.println("3.使用教程 & 应用WebDriver");
        System.out.println("输入任意键退出");
    }

    private void scannerHelp() {

        Scanner scanner = new Scanner(System.in);
        System.out.println("====================使用教程====================");
        System.out.println("||  1.下载chrome & 安装");
        System.out.println("||  https://www.google.cn/intl/zh-CN/chrome");
        System.out.println("||  ");
        System.out.println("||  2.安装WebDriver");
        System.out.println("||  ");
        System.out.println("||  Windows版本教程: ");
        System.out.println("||  https://blog.csdn.net/m0_37381034/article/details/81603371");
        System.out.println("||  ");
        System.out.println("||  Mac版本教程: ");
        System.out.println("||  https://blog.csdn.net/weixin_41635750/article/details/107829636");
        System.out.println("||  ");
        System.out.println("||  3.配置WebDriver路径");
        System.out.println("||=============================================");
        System.out.println();
        System.out.println("请输入WebDriver的路径:");

        String webDriverPath = scanner.nextLine();
        getAppProps().setProperty("webdriver.chrome.driver", webDriverPath);
        storeProp();
        System.out.println("应用WebDriver路径成功!");
    }

    private void scannerSetUserInfo() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("=========当前用户信息=========");
        System.out.println("||  用户名: " + getUserInfo().getUsername());
        System.out.println("||  密码: " + getUserInfo().getPassword());
        System.out.println("||  姓名: " + getUserInfo().getFullName());
        System.out.println("============================");

        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入姓名");
        String fullName = scanner.nextLine();

        setUsername(username);
        setPassword(password);
        setUserFullName(fullName);
        storeProp();
        initProp();

        System.out.println("用户信息设置成功");
    }

    /**
     * 登录jira
     */
    @Override
    protected boolean login() throws Exception {

        WebElement userNameInput = driver.findElement(By.cssSelector("#login-form-username"));
        userNameInput.click();
        userNameInput.sendKeys(userInfo.getUsername());

        WebElement passwordInput = driver.findElement(By.cssSelector("#login-form-password"));
        passwordInput.click();
        passwordInput.sendKeys(userInfo.getPassword());

        //点击登录
        driver.findElement(By.cssSelector("#login")).click();
        Thread.sleep(1000);

        if (SeleniumUtils.existElement(driver, By.cssSelector("#usernamerror-icon"))) {
            System.out.println("用户名和密码不正确,请重新设置用户名和密码");
            return false;
        }
        return true;
    }

    public void setUsername(String username) {
        getAppProps().setProperty("app.jira.username", username);
    }

    public void setPassword(String password) {
        getAppProps().setProperty("app.jira.password", password);
    }

    public void setUserFullName(String name) {
        getAppProps().setProperty("app.jira.name", name);
    }

    public void storeProp() {
        getAppProps().store("app.properties");
    }

}
